package net.cohoivang.admin;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.google.gson.JsonObject;

import net.cohoivang.core.Company;
import net.cohoivang.core.Industry;
import net.cohoivang.core.ProfileCompany;
import net.cohoivang.core.ProfileDatabase;
import net.cohoivang.core.Sector;
import net.cohoivang.web.GoldenAction;

public class CreateCompany extends GoldenAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String legalName;
	private String englishName;
	private String shortName;
	private String address1;
	private String phone;
	private String fax;
	private String email;
	private String website;
	private String description;
	private int industry;
	private int sector;
	private String createDate;
	private String updateDate;
	
	private List<Sector> listSector = new LinkedList<Sector>();
	public List<Sector> getListSector() {
		return listSector;
	}

	public void setListSector(List<Sector> listSector) {
		this.listSector = listSector;
	}
	
	public String getLegalName() {
		return legalName;
	}
	public void setLegalName(String legalName) {
		this.legalName = legalName;
	}
	public String getEnglishName() {
		return englishName;
	}
	public void setEnglishName(String englishName) {
		this.englishName = englishName;
	}
	public String getShortName() {
		return shortName;
	}
	public void setShortName(String shortName) {
		this.shortName = shortName;
	}
	public String getAddress1() {
		return address1;
	}
	public void setAddress1(String address1) {
		this.address1 = address1;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getFax() {
		return fax;
	}
	public void setFax(String fax) {
		this.fax = fax;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getWebsite() {
		return website;
	}
	public void setWebsite(String website) {
		this.website = website;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public int getIndustry() {
		return industry;
	}
	public void setIndustry(int industry) {
		this.industry = industry;
	}
	public int getSector() {
		return sector;
	}
	public void setSector(int sector) {
		this.sector = sector;
	}
	public String getCreateDate() {
		return createDate;
	}
	public void setCreateDate(String createDate) {
		this.createDate = createDate;
	}
	public String getUpdateDate() {
		return updateDate;
	}
	public void setUpdateDate(String updateDate) {
		this.updateDate = updateDate;
	}
	@Override
	protected void fillActionJson(JsonObject mainJson) {
		// TODO Auto-generated method stub
		
	}
	public String createCompanyJSP(){
		listSector = ProfileCompany.getInstance().getAllSector();
		return SUCCESS;
	}
	public String doCreateCompany() throws ParseException, SQLException{
		//convert string to sql.date
		DateFormat formater = new SimpleDateFormat("dd/MM/yyyy");  
		java.util.Date parsedUtilDate = formater.parse(createDate);
		java.sql.Date sqltDate = new java.sql.Date(parsedUtilDate.getTime());		
		
		int companyId = ProfileDatabase.getInstance().insertCompany(legalName, englishName, shortName, address1, phone, fax, email, website, description, industry, sqltDate, sqltDate, sector);
		Sector sec = ProfileCompany.getInstance().findSector(sector);
		Industry in = ProfileCompany.getInstance().findIndustry(industry);
		Company company = new Company(companyId, legalName, englishName, shortName, address1, phone, fax,
				email, website, description, in, parsedUtilDate, parsedUtilDate, sec);
		//Date date = df.parse(updateDate);
		
		in.insertCompany(company);
		ProfileCompany.getInstance().insertCompany(companyId, company);
		
		return SUCCESS;
	}
	
	
}
